﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using connect;
using Pharmacy_management_system;
using System.Collections;

namespace Pharmacy_management_system
{
    public partial class denglu : Form
    {
        public denglu()
        {
            InitializeComponent();
        }

        bool user;

        private void yanzheng()
        {
            user = false;
            label4.Text = "";
            SqlConnection con = new SqlConnection(SQL.constr);
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandType = CommandType.Text;
            if (textBox1.Text.Length == 0 | textBox2.Text.Length == 0)
            {
                label4.Text = "系统提示：用户名或密码不能为空，请再次输入。";
                textBox1.Focus();
                return;
            }
            cmd.CommandText = "select count(*) from sys_sys_users where user_name ='" + textBox1.Text + "'";
            con.Open();
            if ((int)cmd.ExecuteScalar() == 0)
            {
                label4.Text = "系统提示：不存在此用户，请检查后重新输入。";
                textBox1.Focus();
                return;
            }
            cmd.CommandText = "select user_name,pass_word from sys_sys_users where pass_word='" + textBox2.Text + "' and user_name='"+textBox1.Text+"'";
            SqlDataReader myReader = cmd.ExecuteReader();
            if (myReader.Read())
            {
                if (textBox2.Text == myReader[1].ToString() && textBox1.Text.Length != 0)
                user = true;
            }

            else
            {
                label4.Text = "系统提示：用户 " + textBox1.Text + "的密码不正确，请检查后重新输入。";
                textBox2.Focus();
                user = false;
                return;
            }

            con.Close();
        }

        
        //登录
        private void button1_Click(object sender, EventArgs e)
        {
            yanzheng();
            if (user)
            {
                SqlConnection con = new SqlConnection(SQL.constr);
                con.Open();
                string cmdText = "select name,title,dept_name from dict_employee where dict_employee.emp_sn=(select emp_sn from  sys_sys_users where user_name='"+textBox1.Text.Trim()+"')";
                SqlCommand one = con.CreateCommand();
                Form1 f1 = new Form1();
                f1.name1 = textBox1.Text;
                DangQianHang.cc = textBox1.Text;//锁屏
                one.CommandText = cmdText;
                SqlDataReader dr = one.ExecuteReader();
                dr.Read();
                f1.name2 = dr[0].ToString();
                f1.title = dr[1].ToString();
                f1.dept_name = dr[2].ToString();
                f1.Show();
                this.Visible = false;
            }
           
        }
        
        //取消
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //修改密码
        private void button3_Click(object sender, EventArgs e)
        {
            yanzheng();
            if (user)
            {
                PassSystem.UserID = textBox1.Text;
                Form xiugai = new mimaxiugai();
                xiugai.ShowDialog();
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                textBox2.Focus();
            }
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button1_Click(sender, e);
            }

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void denglu_Load(object sender, EventArgs e)
        {

        }
    }
}
